package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.redis.RedisService.IUserInfoRedisService;
import cn.wolfcode.luowowo.repository.UserInfoRepository;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.AssertUtil;
import cn.wolfcode.luowowo.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
//@Transactional
public class IUserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    //获取某个客户的信息
    @Override
    public UserInfo get(String id) {
        Optional<UserInfo> byId = userInfoRepository.findById(id);
        if (byId.isPresent()) {
            return byId.get();
        }
        return null;
    }

    //查看账户是否注册
    @Override
    public boolean checkPhone(String phone) {
        UserInfo userInfo = userInfoRepository.findByPhone(phone);
        //如果被注册了就返回ture，否则就返回false
        return userInfo != null;

    }

    //发送验证码
    @Override
    public void sendVerifyCode(String phone, String smsUrl, String appkey) {

        //生成验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        //发送信息
        StringBuilder sb = new StringBuilder(80);
        sb.append("【创信】你的验证码是：").append(code).append("请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用");


        //真实发送短信
        RestTemplate ret = new RestTemplate();
        //参数一：请求路径
        // String url="https://way.jd.com/chuangxin/dxjk?mobile={1}&content={2}&appkey={3}";
        //参数二：请求返回类型
        //参数三:请求参数
        //String appkey = "69244eaba9f2b6a686ecdf530b3e1984";
        System.out.println(sb.toString());
        String result = ret.getForObject(smsUrl, String.class, phone, sb.toString(), appkey);
        System.out.println(result);
        if (result.contains("Faild")) {
            throw new LogicException("短信发送失败");
        }
        ////缓存信息
        userInfoRedisService.sendVerifyCode(phone, code);
    }

    //注册
    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //1.判断客户数据是否为空
        AssertUtil.hasLength(phone, "号码不能为空");
        AssertUtil.hasLength(nickname, "昵称不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "确认密码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");
        //2.判断密码与重复密码是否一致
        AssertUtil.isEquals(password, rpassword, "密码跟确认密码不一致");

        //3.判断用户是否被注册
        if (this.checkPhone(phone)) {
            throw new LogicException("账号已被注册");
        }
        //4.判断验证码客户输入的验证码跟我们发出的验证码是否一致
        String code = userInfoRedisService.getVerifyCode(phone);
        if (code == null || !code.equalsIgnoreCase(verifyCode)) {
            throw new LogicException("验证码不正确");
        }


        //5.注册用户
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setEmail("");
        userInfo.setPassword(password);
        userInfo.setLevel(1);
        userInfo.setCity("");
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setInfo("");
        userInfo.setState(UserInfo.STATE_NORMAL);
        userInfoRepository.save(userInfo);

    }

    //登录
    @Override
    public UserInfo login(String username, String password) {
        AssertUtil.hasLength(username, "名字不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        UserInfo user = userInfoRepository.findByPhoneAndPassword(username, password);
        if (user == null) {
            throw new LogicException("账户或密码错误");
        }
        user.setPassword(null);//建议将敏感信息为空
        return user;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoRepository.findAll();
    }
    @Override
    public List<UserInfo> findByCity(String keyword) {
        return userInfoRepository.findByCity(keyword);
    }
}
